using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Mathematics;


namespace BriansClimb.Climber
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    /// 

 
    public class BodyPart2 : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Texture2D texture;
        private Body body;
        private Microsoft.Xna.Framework.Vector2 origin;
        private FarseerGames.FarseerPhysics.Mathematics.Vector2 position;
        private Geom geom;
        private PhysicsSimulator physicsSimulator;
        private Game game;
        private string name;
        private int height, weight, width;

        public BodyPart2(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }
        /// <summary>
        /// Constructs the body part based on the passed parameters.
        /// </summary>
        /// <param name="width">Width of the body part.</param>
        /// <param name="height">Height of the body part.</param>
        /// <param name="weight">Weight of the body part.</param>
        /// <param name="tex">Texture of the body part.</param>
        /// <param name="physicsSimulator">PhysicsSimulator to add the body part to.</param>
        /// <param name="positionIn">Initial position of the body part</param>
        /// <returns>PhysicsSimulator</returns>
        public PhysicsSimulator makeBodyPart(int width, int height, int weight, Texture2D tex, PhysicsSimulator physicsSimulator, FarseerGames.FarseerPhysics.Mathematics.Vector2 positionIn, string name)
        {
            this.height = height;
            this.weight = weight;
            this.width = width;
            texture = tex;
            this.name = name;
            this.physicsSimulator = physicsSimulator;
            body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, width,height,weight);
            position = positionIn;
            body.Position = positionIn;
            geom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, body, width,height);
            physicsSimulator.Add(geom);
            origin = new Microsoft.Xna.Framework.Vector2(texture.Width / 2f, texture.Height / 2f);
            return physicsSimulator;
        }

        public PhysicsSimulator makeBodyPart(int width, int height, int weight, Texture2D tex, PhysicsSimulator physicsSimulator, FarseerGames.FarseerPhysics.Mathematics.Vector2 positionIn, string name, int circle)
        {
            this.height = height;
            this.weight = weight;
            this.width = width;
            this.name = name;
            texture = tex;
            this.physicsSimulator = physicsSimulator;
            body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, width, height, weight);
            position = positionIn;
            body.Position = positionIn;
            geom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, body, this.width / 2, 10);
            //geom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, body, width, height);
            physicsSimulator.Add(geom);
            origin = new Microsoft.Xna.Framework.Vector2(texture.Width / 2f, texture.Height / 2f);
            return physicsSimulator;


        }

        public PhysicsSimulator makeBodyPart(int width, int height, PhysicsSimulator physicsSimulator, FarseerGames.FarseerPhysics.Mathematics.Vector2 positionIn)
        {
            this.height = height;
            this.weight = 1;
            this.width = width;
            
            this.physicsSimulator = physicsSimulator;
            body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, width, height, 1);
            position = positionIn;
            body.Position = positionIn;
            geom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, body, width, height);
            physicsSimulator.Add(geom);
            this.setIsHold(true);            
            return physicsSimulator;
        }
        
        /// <summary>
        /// Returns the Body object of the BodyPart.
        /// </summary>
        /// <returns>Body object.</returns>
        public Body getBody()
        {
            return body;
        }
        /// <summary>
        /// Sets the Body object for the BodyPart.
        /// </summary>
        /// <param name="bodyIn">The body to set.</param>
        public void setBody(Body bodyIn)
        {
            body = bodyIn;
        }

        public Microsoft.Xna.Framework.Vector2 getOrigin()
        {
            return origin;
        }

        public void setOrigin(Microsoft.Xna.Framework.Vector2 originIn)
        {
            origin = originIn;
        }

        public Geom getGeom()
        {
            return geom;
        }

        public void setGeom(Geom geomIn)
        {
            geom = geomIn;
        }

        public void setTexture(Texture2D texIn)
        {
            texture = texIn;
        }

        public Texture2D getTexture()
        {
            return texture;
        }

        public string getName()
        {
            return name;
        }

        public void setPosition(FarseerGames.FarseerPhysics.Mathematics.Vector2 positionIn)
        {
            position = positionIn;
        }

        public FarseerGames.FarseerPhysics.Mathematics.Vector2 getPosition()
        {
            return position;
        }

        public BodyPart getBodyPart()
        {
            return this;
        }

        public void setIsHand(bool boolIn)
        {
            if (boolIn == true)
                body.isHand = true;
            else if (boolIn == false)
                body.isHand = false;
        }

        public void setIsHold(bool boolIn)
        {
            if (boolIn == true)
            {
                body.isHold = true;
                body.isStatic = true;
                
            }
            else if (boolIn == false)
                body.isHold = false;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Console.WriteLine("drawing");
            base.Draw(gameTime);
        }
    }
}